package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssm.base.BaseController;
import com.ssm.entity.UserInfo;
import com.ssm.result.Result;
import com.ssm.result.ResultCodeEnum;
import com.ssm.service.UserInfoService;
import com.ssm.util.MD5;
import com.ssm.vo.LoginVo;
import com.ssm.vo.RegisterVo;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/21
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {
    @Reference
    private UserInfoService userInfoService;

    /**
     * 会员注册
     * @param registerVo
     * @param request
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpServletRequest request) {
        String nickName = registerVo.getNickName();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //检验参数
        if(StringUtils.isEmpty(nickName) ||
            StringUtils.isEmpty(phone) ||
            StringUtils.isEmpty(password) ||
            StringUtils.isEmpty(code)){
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //验证码
        String currentCode = (String) request.getSession().getAttribute("CODE");
        if(!code.equals(currentCode)){
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }

        //验证手机号是否注册
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if(null!= userInfo){
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        userInfo = new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPhone(phone);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    /**
     * 验证码
     * @param moble
     * @param request
     * @return
     */
    @GetMapping("/sendCode/{moble}")
    public Result sendCode(@PathVariable("moble") String moble,HttpServletRequest request) {
        String code = "1111";
        request.getSession().setAttribute("CODE",code);
        return Result.ok(code);
    }

    /**
     * 登录
     * @param loginVo
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo,HttpServletRequest request){
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();

        //参数验证
        if(StringUtils.isEmpty(phone)){
            if(StringUtils.isEmpty(password)){
                return Result.build(null,ResultCodeEnum.PARAM_ERROR);
            }
        }

        //账号验证
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if(null == userInfo){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        //验证密码
        if(!MD5.encrypt(password).equals(userInfo.getPassword())){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }

        //验证是否被禁用
        if(userInfo.getStatus() == 0){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }

        request.getSession().setAttribute("USER",userInfo);
        Map<String,Object> map = new HashMap<>();
        map.put("phone",userInfo.getPhone());
        map.put("nickName",userInfo.getNickName());
        return Result.ok(map);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("USER");
        return Result.ok();
    }
}
